//
//  DBEcgRecord.m
//  CareRingApp
//
//  Created by Linktop on 2024/4/7.
//

#import "DBEcgRecord.h"
#import "ConfigModel.h"
#import <MJExtension/MJExtension.h>
#import "SqliteDbTool.h"

const int ECG_MESURING_DURA_SECONDS = 30; //测量时间
extern const NSInteger MAIN_ECG_RES_ALL_DATA;

NSString * const TABLE_NM_ECG = @"table_ecg";


@implementation DBEcgRecord

- (instancetype)initWithMacaddress:(NSString * )mac
{
    self = [super init];
    if (self) {
        self.mac = mac;
        self.wearHand = LEFT_HAND; //默认
    }
    return self;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.wearHand = LEFT_HAND; //默认
    }
    return self;
}

//创建数据库
+(void)createTable
{

    [[DbManager instance] dbQueue:^(sqlite3 * _Nonnull db) { 
        //创建数据库表
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(startTs INTEGER NOT NULL,  macAddress TEXT NOT NULL , endTs INTEGER, avgHeartRate INT, dayFormatString TEXT, firmWare TEXT NOT NULL, isRingAlgorithm BOOLEAN,  conclusion INT, arrhythmia INT , ecgSq INT, low_amplitude  BOOLEAN, significant_noise BOOLEAN, unstable_signal BOOLEAN, not_enough_data BOOLEAN, rmssd INT, sdnn INT, stress INT , bmr INT, activeCal INT, symptomsArray TEXT, volArray TEXT, wearHand INT ,PRIMARY KEY(startTs, macAddress))", TABLE_NM_ECG];
        
        char * err;
        if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
            //失败
//            sqlite3_close(db);
            NSAssert(NO, @"create table fail! %s",err);
        }
        DebugNSLog(@"create table succesful, table: %@", TABLE_NM_ECG);
        
    }];
    
}

-(void)deleteFromTable:(void (^)(void))cmpBlk
{
    WEAK_SELF
    [[DbManager instance] dbQueue:^(sqlite3 * _Nonnull db) {
        STRONG_SELF
        
        NSString *sql =  [NSString stringWithFormat:@"DELETE FROM %@ WHERE startTs = ? AND macAddress = ?", TABLE_NM_ECG];
    
        sqlite3_stmt *statement;
        
        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
            
            if (sqlite3_step(statement) == SQLITE_DONE) {
                DebugNSLog(@"table_ecg record deleted!");
            } else {
                
                DebugNSLog(@"table_ecg record delete error: %s", sqlite3_errmsg(db));
            }
            
            // 释放statement
            sqlite3_finalize(statement);
        } else {
            NSLog(@"准备SQL语句时出错: %s", sqlite3_errmsg(db));
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (cmpBlk) {
                cmpBlk();
            }
        });
       
    }];
}

-(void)updateSymptoms:(void(^)(BOOL succ))complete
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (complete) {
            complete(YES);
        }
    });

}

-(void)insert:(void(^)(BOOL succ))complete
{
    
    WEAK_SELF
    [[DbManager instance] dbQueue:^(sqlite3 * _Nonnull db){
        STRONG_SELF
        
        NSDateFormatter *fmt = [NSDateFormatter new];
        fmt.dateFormat = @"yyyy-MM-dd";
        
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:strongSelf.startTs.doubleValue];
        
        strongSelf.dayFormatString = [fmt stringFromDate:date];
        //INSERT OR IGNORE INTO
        NSString *sqlString = [NSString stringWithFormat:  @"INSERT  INTO %@ (startTs,  macAddress , endTs, avgHeartRate , dayFormatString , firmWare , isRingAlgorithm,  conclusion , arrhythmia  , ecgSq , low_amplitude , significant_noise, unstable_signal , not_enough_data , rmssd , sdnn , stress  , bmr , activeCal , symptomsArray , volArray, wearHand) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", TABLE_NM_ECG];
        
        sqlite3_stmt *statement;
        BOOL succ = NO;
        if (sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &statement, NULL) == SQLITE_OK) {
            // 绑定要插入的数据
            sqlite3_bind_int64(statement, 1, strongSelf.startTs.unsignedIntegerValue);
            sqlite3_bind_text(statement, 2, [strongSelf.mac UTF8String], -1, NULL);
            sqlite3_bind_int64(statement, 3, strongSelf.endTs.unsignedIntegerValue);
            sqlite3_bind_int(statement, 4, strongSelf.avgHeartRate.intValue);
            sqlite3_bind_text(statement, 5, [strongSelf.dayFormatString UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 6, [strongSelf.firmWare UTF8String], -1, NULL);
            sqlite3_bind_int(statement, 7, strongSelf.isRingAlgorithm);
            sqlite3_bind_int64(statement, 8, strongSelf.conclusion);
            sqlite3_bind_int64(statement, 9, strongSelf.arrhythmia);
            sqlite3_bind_int64(statement, 10, strongSelf.ecgSq);
            sqlite3_bind_int(statement, 11, strongSelf.low_amplitude);
            sqlite3_bind_int(statement, 12, strongSelf.significant_noise);
            sqlite3_bind_int(statement, 13, strongSelf.unstable_signal);
            sqlite3_bind_int(statement, 14, strongSelf.not_enough_data);

            sqlite3_bind_int64(statement, 15, strongSelf.rmssd);
            sqlite3_bind_int64(statement, 16, strongSelf.sdnn);
            sqlite3_bind_int64(statement, 17, strongSelf.stress);
            sqlite3_bind_int64(statement, 18, strongSelf.bmr);
            sqlite3_bind_int64(statement, 19, strongSelf.activeCal);
            sqlite3_bind_text(statement, 20, [[strongSelf.symptomsArray mj_JSONString] UTF8String],-1, NULL);
            sqlite3_bind_text(statement, 21, [[strongSelf.volArray mj_JSONString] UTF8String],-1, NULL);
            sqlite3_bind_int64(statement, 22, strongSelf.wearHand);


            
            // 执行SQL语句
            if (sqlite3_step(statement) == SQLITE_DONE) {
                DebugNSLog(@"insert succ %s %d", __FILE__, __LINE__);
                succ= YES;
            } else {
                DebugNSLog(@"insert errror: %s", sqlite3_errmsg(db));
            }
            
            // 释放statement
            sqlite3_finalize(statement);
        } else {
            DebugNSLog(@"SQL error: %s", sqlite3_errmsg(db));
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (complete) {
                complete(succ);
            }
        });
        
    }];
}


/// 查询各种结论的计数
/// - Parameters:
///   - macAddress: 账号
///   - cmpBlk: 完成
+(void)queryAllResultCounts:(NSString * _Nonnull)macAddress
      Cpmplete:(void(^)(NSMutableDictionary *resultDict))cmpBlk
{
    
    [[DbManager instance] dbQueue:^(sqlite3 * _Nonnull db) {
        
        NSMutableDictionary *resultDict = [NSMutableDictionary new];


        NSString *sql =  [NSString stringWithFormat:@"SELECT conclusion, COUNT(conclusion) as cntConclu  FROM %@ WHERE macAddress = ? GROUP BY conclusion", TABLE_NM_ECG];
        sqlite3_stmt *stmt;

//        FMResultSet *rs = [db executeQuery:sql, macAddress];
        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
            sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);
           
            SqliteDbTool *tool = [[SqliteDbTool alloc]initWithStmt:stmt];
//            NSMutableDictionary *map = [self columnNameToIndexMap:stmt];
            
            while (sqlite3_step(stmt) == SQLITE_ROW)
            {
                ECG_CONCLUSION conclusion = [tool intForColumn:@"conclusion"];
                int count = [tool intForColumn:@"cntConclu"];
                resultDict[@(conclusion)] = @(count);

                
            }

        }
        sqlite3_finalize(stmt);
        stmt = NULL;
        

        NSString *sqlAll =  [NSString stringWithFormat:@"SELECT  COUNT(*) as cnt FROM %@ WHERE macAddress = ? ", TABLE_NM_ECG];
        if (sqlite3_prepare_v2(db, [sqlAll UTF8String], -1, &stmt, nil) == SQLITE_OK) {
            sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);
            SqliteDbTool *tool = [[SqliteDbTool alloc]initWithStmt:stmt];

            while (sqlite3_step(stmt) == SQLITE_ROW)
            {
                int count = [tool intForColumn:@"cnt"];
                resultDict[@(MAIN_ECG_RES_ALL_DATA)] = @(count);
            }
        }
        sqlite3_finalize(stmt);

        if (cmpBlk) {
            dispatch_async(dispatch_get_main_queue(), ^{
                cmpBlk(resultDict);
            });
        }
       
    }];
}



+(void)queryByTimeRange:(NSString * _Nonnull)macAddress 
              BeginDate:(NSDate *)beginDate
                EndDate:(NSDate *)enDate
        OrderBeTimeDesc:(BOOL)isDesc
                  Limit:(NSUInteger)limit
      Cpmplete:(void(^)(NSMutableArray<DBEcgRecord *> *results))cmpBlk
{
    
    [[DbManager instance] dbQueue:^(sqlite3 * _Nonnull db) {

        NSString *sql =  [NSString stringWithFormat:@"SELECT rowid , * FROM %@ WHERE macAddress = ? AND (startTs >= ? AND startTs < ?)  ORDER BY startTs %@ Limit %lu", TABLE_NM_ECG, isDesc ? @"DESC" : @"ASC", limit];
        
//        FMResultSet *rs = [db executeQuery:sql, macAddress, beginDate, enDate];
        sqlite3_stmt *stmt;

        NSMutableArray<DBEcgRecord *> *results = [NSMutableArray new];
        
        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
            sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);
            sqlite3_bind_double(stmt, 2, beginDate.timeIntervalSince1970);
            sqlite3_bind_double(stmt, 3, enDate.timeIntervalSince1970);

            SqliteDbTool *tool = [[SqliteDbTool alloc]initWithStmt:stmt];
            
            while (sqlite3_step(stmt) == SQLITE_ROW) {
                
                NSString * account = [tool stringForColumn:@"account"];
                DBEcgRecord *record = [[DBEcgRecord alloc]initWithMacaddress:account];
                /*
                  low_amplitude , significant_noise, unstable_signal , not_enough_data , rmssd , sdnn , stress  , bmr , activeCal , symptomsArray , volArray
                 */
                record.startTs = @([tool doubleForColumn:@"startTs"]);
                record.endTs = @([tool doubleForColumn:@"endTs"]);
                record.avgHeartRate = @([tool intForColumn:@"avgHeartRate"]);
                record.dayFormatString = [tool stringForColumn:@"dayFormatString"];
                record.firmWare = [tool stringForColumn:@"firmWare"];
                record.isRingAlgorithm = [tool boolForColumn:@"isRingAlgorithm"];
                record.conclusion = [tool intForColumn:@"conclusion"];
                record.arrhythmia = [tool intForColumn:@"arrhythmia"];
                record.ecgSq = [tool intForColumn:@"ecgSq"];
                record.low_amplitude = [tool boolForColumn:@"low_amplitude"];
                record.significant_noise = [tool boolForColumn:@"significant_noise"];
                record.unstable_signal = [tool boolForColumn:@"unstable_signal"];
                record.not_enough_data = [tool boolForColumn:@"not_enough_data"];
                record.rmssd = [tool intForColumn:@"rmssd"];
                record.sdnn = [tool intForColumn:@"sdnn"];
                record.stress = [tool intForColumn:@"stress"];
                record.bmr = [tool intForColumn:@"bmr"];
                record.activeCal = [tool intForColumn:@"activeCal"];
                
                if (![tool columnIsNull:@"symptomsArray"]) {
                    NSString *str = [tool stringForColumn:@"symptomsArray"];
                    
                    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
                    record.symptomsArray =  [NSMutableArray arrayWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]];
                
                }
                
                if (![tool columnIsNull:@"volArray"]) {
                    NSString *str = [tool stringForColumn:@"volArray"];
                    
                    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
                    record.volArray =  [NSMutableArray arrayWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]];
                
                }
                
                if (![tool columnIsNull:@"wearHand"]) {
                    record.wearHand = [tool intForColumn:@"wearHand"];
                }
                
                            
                [results addObject:record];
               
            }

            
        }
        sqlite3_finalize(stmt);

       
        if (cmpBlk) {
            dispatch_async(dispatch_get_main_queue(), ^{
                cmpBlk(results);
            });
        }
       
    }];
}

+(void)queryAllDatesOrderByday:(NSString *)macAddress BLK:(void(^)(NSMutableDictionary * dateKeys ))cmpBlk;
{
    
//    [[HMDBManager instance].dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
//        
//        NSString *sql =  [NSString stringWithFormat:@"SELECT startTs FROM %@ WHERE macAddress = ?", TABLE_NM_ECG];
//        FMResultSet *rs = [db executeQuery:sql, macAddress];
//        
//        NSDateFormatter * dateFormatter = [NSDateFormatter new];
//        dateFormatter.dateFormat = @"yyyy-MM-dd";
//        NSMutableDictionary *dict = [NSMutableDictionary new];
//        while ([rs next]) {
//            NSDate * date = [rs dateForColumn:@"startTs"];
//            NSString *key = [dateFormatter stringFromDate:date];
//            dict[key] = @(1);
//            
//        }
//        
//        [rs close];
//        
//        
//        if (cmpBlk) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                cmpBlk(dict);
//            });
//        }
//        
//    }];
    
}



+(void)queryByConclusion:(NSString * _Nonnull)macAddress
              Conclusion:(NSNumber *)conclusion
              BeginDate:(NSDate *)beginDate
                EndDate:(NSDate *)enDate
        OrderBeTimeDesc:(BOOL)isDesc
      Cpmplete:(void(^)(NSMutableArray<DBEcgRecord *> *results))cmpBlk
{
    
    [[DbManager instance] dbQueue:^(sqlite3 * _Nonnull db) {

        
//        [db beginTransaction];
        NSString *sql;
//        FMResultSet *rs;
        
        if (enDate) {
            if (conclusion) {
                sql =  [NSString stringWithFormat:@"SELECT rowid , * FROM %@ WHERE macAddress = ? AND (startTs >= ? AND startTs < ?) AND (conclusion = ?)  ORDER BY startTs %@", TABLE_NM_ECG, isDesc ? @"DESC" : @"ASC"];
//                rs = [db executeQuery:sql, macAddress, beginDate, enDate, conclusion];
            

            }
            
            if (!conclusion) {
                // 全类型 时间范围
                sql =  [NSString stringWithFormat:@"SELECT rowid , * FROM %@ WHERE macAddress = ? AND (startTs >= ? AND startTs < ?)  ORDER BY startTs %@", TABLE_NM_ECG, isDesc ? @"DESC" : @"ASC"];
               
               
//                rs = [db executeQuery:sql, macAddress, beginDate, enDate];
               
            }
        }
        

        if (!enDate) {
            if (conclusion) {
                // 按类型全查
                sql =  [NSString stringWithFormat:@"SELECT rowid , * FROM %@ WHERE macAddress = ?  AND (conclusion = ?)  ORDER BY startTs %@", TABLE_NM_ECG, isDesc ? @"DESC" : @"ASC"];
               
//                rs = [db executeQuery:sql, macAddress, conclusion];
             
            }
            
            if (!conclusion) {
                // 全类型 全查
                sql =  [NSString stringWithFormat:@"SELECT rowid , * FROM %@ WHERE macAddress = ?  ORDER BY startTs %@", TABLE_NM_ECG, isDesc ? @"DESC" : @"ASC"];
               
//                rs = [db executeQuery:sql, macAddress];
            }
        }
        
       
            
        
        NSMutableArray<DBEcgRecord *> *results = [NSMutableArray new];
        
        sqlite3_stmt *stmt;

        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK) {
            if (enDate) {
                if (conclusion) {
                    
                    sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);
                    sqlite3_bind_double(stmt, 2, beginDate.timeIntervalSince1970);
                    sqlite3_bind_double(stmt, 3, enDate.timeIntervalSince1970);
                    sqlite3_bind_int(stmt, 4, conclusion.intValue);
                }
                if (!conclusion) {
                    sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);
                    sqlite3_bind_double(stmt, 2, beginDate.timeIntervalSince1970);
                    sqlite3_bind_double(stmt, 3, enDate.timeIntervalSince1970);
                }
            }
            if (!enDate) {
                if (conclusion) {
                    sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);
                    sqlite3_bind_int(stmt, 2, conclusion.intValue);
                }
                if (!conclusion) {
                    sqlite3_bind_text(stmt, 1, [macAddress UTF8String], -1, NULL);

                }
            }
            
            SqliteDbTool *tool = [[SqliteDbTool alloc]initWithStmt:stmt];
            
            while (sqlite3_step(stmt) == SQLITE_ROW) {
                NSString * macAddress = [tool stringForColumn:@"macAddress"];
                DBEcgRecord *record = [[DBEcgRecord alloc]initWithMacaddress:macAddress];
              
                record.startTs = @([tool doubleForColumn:@"startTs"]);
                record.endTs = @([tool doubleForColumn:@"endTs"]);
                record.avgHeartRate = @([tool intForColumn:@"avgHeartRate"]);
                record.dayFormatString = [tool stringForColumn:@"dayFormatString"];
                record.firmWare = [tool stringForColumn:@"firmWare"];
                record.isRingAlgorithm = [tool boolForColumn:@"isRingAlgorithm"];
                record.conclusion = [tool intForColumn:@"conclusion"];
                record.arrhythmia = [tool intForColumn:@"arrhythmia"];
                record.ecgSq = [tool intForColumn:@"ecgSq"];
                record.low_amplitude = [tool boolForColumn:@"low_amplitude"];
                record.significant_noise = [tool boolForColumn:@"significant_noise"];
                record.unstable_signal = [tool boolForColumn:@"unstable_signal"];
                record.not_enough_data = [tool boolForColumn:@"not_enough_data"];
                record.rmssd = [tool intForColumn:@"rmssd"];
                record.sdnn = [tool intForColumn:@"sdnn"];
                record.stress = [tool intForColumn:@"stress"];
                record.bmr = [tool intForColumn:@"bmr"];
                record.activeCal = [tool intForColumn:@"activeCal"];
                
                if (![tool columnIsNull:@"symptomsArray"]) {
                    NSString *str = [tool stringForColumn:@"symptomsArray"];
                    
                    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
                    record.symptomsArray =  [NSMutableArray arrayWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]];
                
                }
                
                if (![tool columnIsNull:@"volArray"]) {
                    NSString *str = [tool stringForColumn:@"volArray"];
                    
                    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
                    record.volArray =  [NSMutableArray arrayWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]];
                
                }
                            
                [results addObject:record];
            }
            
        }
        
        sqlite3_finalize(stmt);

        if (cmpBlk) {
            dispatch_async(dispatch_get_main_queue(), ^{
                cmpBlk(results);
            });
        }
       
    }];
}



-(void)setValuesWith:(SrEcgAlgorithmResults *) ecgRes
{
    self.arrhythmia = ecgRes.arrhythmia; // 心率检查结果

    self.ecgSq = ecgRes.ecgSq; // 信号质量

    // 心电图信号振幅很低，请确保接触面清洁，然后重试。
    self.low_amplitude = ecgRes.low_amplitude;

    //心电图信号有明显的噪音，请确保设备没有握得太紧，然后重试。
    self.significant_noise = ecgRes.significant_noise;

    //心电图信号似乎不稳定，请确保您保持静止，然后重试。
    self.unstable_signal = ecgRes.unstable_signal;

    //没有足够的数据，请确保遵循心电图记录指南，然后重试。
    self.not_enough_data = ecgRes.not_enough_data;

    self.rmssd = ecgRes.rmssd ;
    self.sdnn = ecgRes.sdnn;

    self.stress = ecgRes.stress; // 压力指数 0-100有效 0XFF表示无效

    self.bmr = ecgRes.bmr; // basal metabolic rate, Unit: Cards per minute(基础代谢率, 单位：卡每分钟)

    self.activeCal = ecgRes.activeCal; // Active energy consumption, Unit: Cards per minute(有功能量消耗, 单位：卡每分钟)
    [self transToConclusion:ecgRes.arrhythmia]; // 心率检查结论
}


-(NSMutableAttributedString *)symptomsPdfAttrstring {
    if (!self.symptomsArray.count) {
        return nil;
    }
    // 添加圆点
    NSString *bulletPoint = @"\u2022";
    NSString *bulletPointString = [NSString stringWithFormat:@"%@ ", bulletPoint];
    NSDictionary *bulletPointAttribute = @{NSParagraphStyleAttributeName: [NSParagraphStyle defaultParagraphStyle], NSFontAttributeName: [UIFont systemFontOfSize:14]};
    NSAttributedString *bulletPointAttributedString = [[NSAttributedString alloc] initWithString:bulletPointString attributes:bulletPointAttribute];
    
    
    NSMutableAttributedString *totalAttreiString = [NSMutableAttributedString new];
    [self.symptomsArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *titleString = [NSString stringWithFormat:@"%@\r\n", [[self class] titleOfSymptom:obj.integerValue]];
        
        // 创建一个NSMutableAttributedString
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]}];
        // 将圆点添加到文字前面
        [attributedString insertAttributedString:bulletPointAttributedString atIndex:0];
        [totalAttreiString appendAttributedString:attributedString];
    }];
    return totalAttreiString;

    
}



+(NSString *)titleOfSymptom:(ECG_SYMPTOM)symptom
{
 
    NSString *desc = nil;
//    switch (symptom) {
//        case SYMPTOM_NONE:
//        {
//            desc = _L(L_SYMPTOM_NONE);
//        }
//            break;
//        case SYMPTOM_RAPID_HEART:
//        {
//            desc = _L(L_SYMPTOM_RAPID_HEART);
//        }
//            break;
//        case SYMPTOM_SKIP_HEARTBEAT:
//        {
//            desc = _L(L_SYMPTOM_SKIP_HEARTBEAT);
//
//        }
//            break;
//        case SYMPTOM_FATIGUE:
//        {
//            desc = _L(L_SYMPTOM_FATIGUE);
//
//        }
//            break;
//        case SYMPTOM_SHORTNESS_OF_BREATH:
//        {
//            desc = _L(L_SYMPTOM_SHORTNESS_OF_BREATH);
//
//        }
//            break;
//        case SYMPTOM_CHEST_TIGHTESS_OR_PAIN:
//        {
//            desc = _L(L_SYMPTOM_CHEST_TIGHTESS_OR_PAIN);
//
//        }
//            break;
//        case SYMPTOM_FAINTING:
//        {
//            desc = _L(L_SYMPTOM_FAINTING);
//
//        }
//            break;
//        case SYMPTOM_DIZZNESS:
//        {
//            desc = _L(L_SYMPTOM_DIZZNESS);
//        }
//            break;
//        case SYMPTOM_OTHER:
//        {
//            desc = _L(L_SYMPTOM_OTHER);
//        }
//            break;
//        default:
//            break;
//    }
    return desc;
    
}

+(NSString *)titleOfConclusion:(ECG_CONCLUSION)conclusion 
{

    NSString *string = @"";
    switch (conclusion) {
        case CONCLUSION_SINUS_RHYTHM:
        {
            string = _L(L_SINUS_RHYTHM);
        }
            break;
        case CONCLUSION_AFI:
        {
            string = _L(L_AFI );
        }
            break;
        case CONCLUSION_HIGH_HR:
        {
            string = _L(L_H_HR );
        }
            break;
        case CONCLUSION_LOW_HR:
        {
            string = _L( L_L_HR);
        }
            break;
        case CONCLUSION_IN_CONCLUSIVE:
        {
            string = _L( L_ECG_INCONCLUSIVE);
        }
            break;
        case CONCLUSION_POOR_RECORDING:
        {
            string = _L( L_POOR_RECORD);
        }
            break;
        case CONCLUSION_NOT_ENOUGH_TIME:
        {
            string = [NSString stringWithFormat: _L( L_NOT_ENOUGH_TIME), ECG_MESURING_DURA_SECONDS ];
        }
            break;
        default:
            break;
    }
    return string;
}
+(NSString *)descOfConclusion:(ECG_CONCLUSION)conclusion
{
    NSString *string = @"";
    switch (conclusion) {
        case CONCLUSION_SINUS_RHYTHM:
        {
            string = _L( L_SINUS_RHYTHM_DESC);
        }
            break;
        case CONCLUSION_AFI:
        {
            string = _L(L_AFI_DESC );
        }
            break;
        case CONCLUSION_HIGH_HR:
        {
            string = _L(L_H_HR_DESC );
        }
            break;
        case CONCLUSION_LOW_HR:
        {
            string = _L(L_L_HR_DESC );
        }
            break;
        case CONCLUSION_IN_CONCLUSIVE:
        {
            string = _L(L_ECG_INCONCLUSIVE_DESC );
        }
            break;
        case CONCLUSION_POOR_RECORDING:
        {
            string = _L(L_POOR_RECORD_DESC );
        }
            break;
        case CONCLUSION_NOT_ENOUGH_TIME:
        {
            string = [NSString stringWithFormat:_L(L_NOT_ENOUGH_TIME_DESC ), ECG_MESURING_DURA_SECONDS, ECG_MESURING_DURA_SECONDS];
        }
            break;
        default:
            break;
    }
    return string;
}

-(void)transToConclusion:(ECG_Arrhythmia)rhythm
{
   
    switch (rhythm) {
        case Arrhythmia_NO_FINNISH:
        {
            self.conclusion = CONCLUSION_IN_CONCLUSIVE;
        }
            break;
        case Arrhythmia_NORMAL:
        {
            self.conclusion = CONCLUSION_SINUS_RHYTHM;
        }
            break;
        case Arrhythmia_Bradycardia:
        {
            self.conclusion = CONCLUSION_LOW_HR;
        }
            break;
        case Arrhythmia_Tachycardia:
        {
            self.conclusion = CONCLUSION_HIGH_HR;
        }
            break;
        case Arrhythmia_NOT_ENOUGH_DATA:
        {
            self.conclusion = CONCLUSION_POOR_RECORDING;//CONCLUSION_NOT_ENOUGH_TIME;
        }
            break;
        case Arrhythmia_Abnormal_unknowReason:
        {
            self.conclusion = CONCLUSION_POOR_RECORDING;
        }
            break;
        case Arrhythmia_AFib:
        {
            self.conclusion = CONCLUSION_AFI;
        }
            break;
        default:
            break;
    }
    
}

-(NSMutableArray<NSNumber *> *)volArray
{
    if (!_volArray) {
        _volArray = [NSMutableArray new];
    }
    return _volArray;
}





@end
